package com.dgut.controller;

import com.dgut.dto.Result;
import com.dgut.dto.UserInfoAndDailyForm;
import com.dgut.pojo.DailyForm;
import com.dgut.pojo.JwtProperties;
import com.dgut.pojo.UserInfo;
import com.dgut.service.impl.DailyFormServiceImpl;
import com.dgut.utils.JwtTokenUtil;
import com.dgut.utils.MyDateUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;


@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    DailyFormServiceImpl dailyFormServiceImpl;
    @Resource
    JwtProperties jwtProperties;
    @Resource
    private JwtTokenUtil jwtTokenUtil;

    //查询最新的表单数据
    @CrossOrigin(origins ="*",maxAge = 3600)
    @GetMapping("/getLatestFormInfo")
    public Result getLatestFormInfo(HttpServletRequest request){
        Result result = new Result();
        String token = request.getHeader(jwtProperties.getHeader());
        String username = jwtTokenUtil.getUsernameFromToken(token);
        UserInfoAndDailyForm userInfoAndDailyForm = dailyFormServiceImpl.getLatestFormWithUserInfo(username);
        result.setMsg("查到最新的数据");
        result.setData(userInfoAndDailyForm);
        return result;
    }

    //查询当天是否已经提交
    @CrossOrigin(origins ="*",maxAge = 3600)
    @GetMapping("/getFillStatus")
    public Result getFillStatus(HttpServletRequest request){
        Result result = new Result();
        String token = request.getHeader(jwtProperties.getHeader());
        String username = jwtTokenUtil.getUsernameFromToken(token);
        String date = MyDateUtil.getSimpleDate(new Date());
        DailyForm formByDate = dailyFormServiceImpl.getFormByDate(username, date);
        if(formByDate!=null){
            result.setCode(0);
            HashMap<String, Object> map = new HashMap<>();
            map.put("fillingDays",formByDate.getFillingDays());
            result.setData(map);
            result.setMsg("今天已经提交信息");
        }
        else{
            result.setCode(200);
            result.setMsg("今天还未提交信息");
        }
        return result;
    }

    //进行填报
    @CrossOrigin(origins ="*",maxAge = 3600)
    @PostMapping("/chockIn")
    public Result clockIn(HttpServletRequest request, @RequestBody UserInfoAndDailyForm userInfoAndDailyForm){
        UserInfo userInfo = userInfoAndDailyForm.getUserInfo();
        DailyForm dailyForm = userInfoAndDailyForm.getDailyForm();
        Result result = new Result();
        String username = jwtTokenUtil.getUsernameFromToken(request.getHeader("Authorization"));
        String date = MyDateUtil.getSimpleDate(new Date());
        DailyForm formByDate = dailyFormServiceImpl.getFormByDate(username, date);
        if(formByDate!=null){
            result.setCode(0);
            result.setMsg("今天已填写");
            return result;
        }
        try {
            int fillingDays = dailyFormServiceImpl.chockIn(username,userInfo,dailyForm) ;
            result.setCode(200);
            result.setMsg("填报成功！");
            HashMap<String, Object> map = new HashMap<>();
            map.put("fillingDays",fillingDays);
            result.setData(map);
        } catch (Exception e) {
            result.setCode(-1);
            result.setMsg("填报失败");
        }
        return result;
    }


}

